package com.baizhi.controller;

import cn.afterturn.easypoi.excel.ExcelExportUtil;
import cn.afterturn.easypoi.excel.ExcelImportUtil;
import cn.afterturn.easypoi.excel.entity.ExportParams;
import cn.afterturn.easypoi.excel.entity.ImportParams;
import cn.afterturn.easypoi.excel.entity.enmus.ExcelType;
import com.baizhi.entity.CmfzGuru;
import com.baizhi.service.CmfzGuruService;
import org.apache.poi.ss.usermodel.Workbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.*;
import java.util.*;

/**
 * @Author: renpu
 * @Date: 2019/2/27 22:24
 * @Description:
 */
@Controller
@RequestMapping("/Guru")
public class CmfzGuruController {
    @Autowired
    private CmfzGuruService guruService;

    @RequestMapping("/showGuru")
    @ResponseBody
    public Map showGuru(int rows,int page){
        return guruService.selectByPage(rows,page);
    }

    @RequestMapping("/addGuru")
    public Map addGuru(CmfzGuru guru){
        Map map=new HashMap();
        try{
            guruService.insertGuru(guru);
            map.put("isadd",true);
        }catch(Exception e){
            e.printStackTrace();
            map.put("isadd",false);
        }
        return map;
    }

    @RequestMapping("/updateGuru")
    public void updateGuru(int[] ids){
        guruService.deleteGuru(ids);
    }



    @RequestMapping("/autoOutTable")
    public void outTable(HttpServletResponse response) throws Exception {
        System.out.println("jinsdfjsadfasd");
        List<CmfzGuru> list=guruService.selectAllGuru();
        /*List<String> strings= Arrays.asList("Id值","上师真实姓名","上师图片","上师法名","法师状态");

        HSSFWorkbook workbook=new HSSFWorkbook();
        HSSFSheet sheet=workbook.createSheet("guruAll");
        HSSFRow row=sheet.createRow(0);

        for (int i=0;i<strings.size();i++){
            HSSFCell cell=row.createCell(i);
            cell.setCellValue(strings.get(i));
        }

        for (int i=0;i<list.size();i++){
            Guru guru=list.get(i);
            HSSFRow row1=sheet.createRow(i+1);

            HSSFCell cell=row1.createCell(0);
            cell.setCellValue(guru.getGuruId());
            HSSFCell cell1=row1.createCell(1);
            cell1.setCellValue(guru.getGuruName());
            HSSFCell cell2=row1.createCell(2);
            cell2.setCellValue(guru.getGuruImage());
            HSSFCell cell3=row1.createCell(3);
            cell3.setCellValue(guru.getGuruNickName());
            HSSFCell cell4=row1.createCell(4);
            cell4.setCellValue(guru.getGuruState());

        }*/
        ExportParams exportParams = new ExportParams("法师表","guruAll", ExcelType.HSSF);
        //1.导出参数对象 1.普通Java类的类对象（要导出的实体类的类对象） 3.一个集合 查询全部的用户信息
        Workbook workbook = ExcelExportUtil.exportExcel(exportParams, CmfzGuru.class,list);

        String fileName = "guruAll.xls";
        response.setCharacterEncoding("UTF-8");
//response.setContentType("application/vnd.ms-excel");
        response.setContentType("application/octet-stream;charset=UTF-8");
        fileName = new String(fileName.getBytes("UTF-8"), "iso-8859-1"); // 为了解决中文名称乱码问题
        response.setHeader("Content-Disposition", "attachment;filename="+ fileName);
        /*workbook.write(new FileOutputStream(new File("E://guru.xlsx")));*/
        workbook.write(response.getOutputStream());

    }

    @RequestMapping("writeTable")
    public void writeTable(MultipartFile file) throws Exception {
     System.out.println("file = " + file);

     //获取源文件的名称；
     String originalFilename = file.getOriginalFilename();
        System.out.println(originalFilename);

        //通过文件对象获取输入流的对象；
    InputStream inputStream=file.getInputStream();
        ImportParams importParams = new ImportParams();

        //将文件的标题哪一行封装进imParams对象里
        importParams.setHeadRows(1);

        //将文件的每列的标题装进imParams对象里
        importParams.setTitleRows(1);

     /*HSSFWorkBook WorkBook=new HSSFWorkBook(inputStream);*/

        List<CmfzGuru> list=ExcelImportUtil.importExcel(inputStream,CmfzGuru.class,importParams);


        System.out.println(list);

       /* HSSFSheet sheet = workbook.getSheet("guruAll");
//        4.获取行 1----最后一行
        int lastRowNum = sheet.getLastRowNum();
//        5.获取数据封装在集合中
        for (int i = 1; i <= lastRowNum; i++) {
            Guru guru = new Guru();
//            获取行
            HSSFRow row = sheet.getRow(i);

//            获取单元格和数据
            HSSFCell cell0 = row.getCell(0);
            double numericCellValue = cell0.getNumericCellValue();
            int id = (int) numericCellValue;
            guru.setGuruId(id);

            HSSFCell cell1 = row.getCell(1);
            String name = cell1.getStringCellValue();
            guru.setGuruName(name);

            HSSFCell cell2 = row.getCell(2);
            String url = cell2.getStringCellValue();
            guru.setGuruImage(url);

            HSSFCell cell3 = row.getCell(3);
            String nickName = cell3.getStringCellValue();
            guru.setGuruNickName(nickName);

            HSSFCell cell4 = row.getCell(4);
            double numericCellValue1 = cell4.getNumericCellValue();
            int status = (int) numericCellValue1;
            guru.setGuruState(status);
                   list.add(guru);

       }*/
        for (CmfzGuru e:list) {
            System.out.println(e);
           guruService.insertGuru(e);

        }
    }

}
